// pages/settle/settle.js
var appData = getApp()
Page({


  /**
   * 页面的初始数据
   */
  data: {
    totalPay: '',
    bkorder: [],
    deAddr: '',
    deName: '',
    deCall: '',
    noSet: true,
    myIntegral: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 发送请求获取用户id
    wx.request({
      url: 'http://124.222.95.170:21311/user/getMyId',
      method: 'GET',
      header: {
        'Cookie': wx.getStorageSync('sessionid')
      },
      success: (res) => {
        wx.setStorageSync('userID', res.data.data)
      }
    })
    console.log('id', wx.getStorageSync('userID'))
    //获取用户积分
    wx.request({
      url: `http://124.222.95.170:21311/user/${wx.getStorageSync('userID')}`,
      method: 'GET',
      header: {
        'Cookie': wx.getStorageSync('sessionid')
      },
      success: (res) => {
        this.setData({
          myIntegral: res.data.data.integral
        })
        console.log(this.data.myIntegral)

      }
    })

    this.setData({
      totalPay: options.totalScore,
      bkorder: wx.getStorageSync('bookCarts'),
    })
    
  },
  //点击去支付
  payBook(e) {
    //如果积分足够
    if (this.data.totalPay < this.data.myIntegral) {
      // wx.showToast({
      //   title: '即将支付这笔订单，请输入支付密码',
      //   icon:'none'
      // })
      this.setData({
        show: true
      })

    } else {
      console.log(this.data.totalPay,'hhh',this.data.myIntegral)
      wx.showModal({
        title: '支付订单',
        content: '您的积分不足，是否充值？',
        success: (res) => {
          if (res.confirm) {
            console.log('用户点击确定')
            wx.navigateTo({
              url: '/pages/wallet/wallet',
            })

          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }

      })
    }

  },
  //关闭上弹框
  onClose() {
    this.setData({
      show: false
    });
  },
  //提交表单
  formsubmit(e) {
    console.log('form发生了submit事件，携带数据为：', e.detail.value)
    wx.request({
      url: `http://124.222.95.170:21311/shoppingCart/checkPassword?password=${e.detail.value.input}`,
      method: 'get',
      header: {
        'Cookie': wx.getStorageSync('sessionid')
      },
      success: (res) => {
        console.log('res', res.data)
        if (res.data.msg == '密码错误') {
          wx.showToast({
            title: '密码错误',
            icon: 'error'
          })
        } else {
          console.log('bookorder', this.data.bkorder)
          console.log('ad', this.data.deAddr)
          let ids = []
          for (let i = 0; i < this.data.bkorder.length; i++) {
            ids.push(this.data.bkorder[i].goods.id)

          }
          wx.request({
            url: 'http://124.222.95.170:21311/shoppingCart/payment',
            method: 'POST',
            header: {
              'Cookie': wx.getStorageSync('sessionid')
            },
            data: {
              "ids": ids,
              "addressId": this.data.deAddr.id
            }
          })
          wx.showToast({
            icon: 'success',
            title: '支付成功',
          })
          this.setData({
            show: false
          })

        }
      }
    })
  },



  //设置地址
  setAddr() {
    let Address = wx.getStorageSync('trueAddr')
    if (Address) {
      this.setData({
        deAddr: Address,
        noSet: false
      })
    }

  },
  goAddr() {
    wx.navigateTo({
      url: '/pages/selectAddr/selectAddr'
    })
  },
  /* 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    //展示默认地址
    wx.request({
      url: 'http://124.222.95.170:21311/address',
      method:'GET',
      header: {
        'Cookie': wx.getStorageSync('sessionid')
      },
      success:(res)=>{
        if(res.data.msg != "当前用户无地址"){
          for(let i=0;i<res.data.data.length;i++)
          { 
            let deData = res.data.data[i]
            if(deData.isDefault==1)
            {
              this.setData({
                deAddr: deData,
                noSet: false
              })
              wx.setStorageSync('trueAddr', deData)
            }
          }
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setAddr()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})